<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="姓名" prop="UserName">
          <a-input v-model="entity.UserName" disabled autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="金额" prop="Amount">
          <a-input v-model="entity.Amount" autocomplete="off" />
        </a-form-model-item>
        <!-- <a-form-model-item label="是否负责人" prop="Principal">
          <a-input
            type="checkbox"
            ref="Principal"
            v-model="entity.Principal"
            @change="onCheckboxChange"
            autocomplete="off"
          />
        </a-form-model-item> -->
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import store from '@/store'

export default {
  props: {
    parentObj: Object,
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 },
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: '',
      rules: {
        Name: [{ required: true, message: '必填' }],
      },
    }
  },
  methods: {
    init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm(item, title) {
      this.init()
      if (item) {
        console.log(item)
        this.entity = item
        if (!this.entity.Principal) {
          this.entity.Principal = this.entity.Principal
        }
      }
    },
    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        console.log(this.entity)
        this.visible = false
      })
    },
    onCheckboxChange() {
      if (!this.entity.Principal) {
        this.entity.Principal = 'true'
        this.$refs.Principal.value = 'true'
      } else if (this.$refs.Principal.value == 'true') {
        this.entity.Principal = 'false'
        this.$refs.Principal.value = 'false'
      } else {
        this.entity.Principal = 'true'
        this.$refs.Principal.value = 'true'
      }
    },
  },
}
</script>
